﻿using System;
using System.Collections.Generic;
using System.Text;
using Common.Utilities.Dispatcher;
using Workflows.Components.Rules.Properties;
using Common.Utilities;
using System.Xml;

namespace Workflows.Components.Rules
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class CodeRuleComponent:WorkflowComponentBase
    {
        #region props
        private CodeRuleInstruction _Instruction;
        /// <summary></summary>
        public override InstructionBase Instruction
        {
            get
            {
                return _Instruction;
            }
            set
            {
                _Instruction =(CodeRuleInstruction) value;
            }
        }

        private CodeRuleOutput _Output;
        /// <summary></summary>
        public override ResultBase Output
        {
            get
            {
                return _Output;
            }
            set
            {
                _Output =(CodeRuleOutput) value;
            }
        }

        /// <summary></summary>
        public string RuleName
        {
            get
            {
                return this._Instruction.RuleName;
            }
            set
            {
                this._Instruction.RuleName = value;
            }
        }

        /// <summary></summary>
        public string CategoryName
        {
            get
            {
                return this._Instruction.CategoryName;
            }
            set
            {
                this._Instruction.CategoryName = value;
            }
        }

        /// <summary></summary>
        public string InputTypeFullName
        {
            get
            {
                return this._Instruction.InputTypeFullName;
            }
            set
            {
                this._Instruction.InputTypeFullName = value;
            }
        }

        /// <summary></summary>
        public string InputAssemblyName
        {
            get
            {
                return this._Instruction.InputAssemblyName;
            }
            set
            {
                this._Instruction.InputAssemblyName = value;
            }
        }

        /// <summary></summary>
        public string Code
        {
            get
            {
                return this._Instruction.Code;
            }
            set
            {
                this._Instruction.Code = value;
            }
        }

        /// <summary></summary>
        public RuleDecision Decision
        {
            get
            {
                return this._Instruction.Decision;
            }
            set
            {
                this._Instruction.Decision = value;
            }
        }

        /// <summary></summary>
        public string SerializedInputObject
        {
            get
            {
                return this._Instruction.SerializedInputObject;
            }
            set
            {
                this._Instruction.SerializedInputObject = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public CodeRuleComponent()
            : base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Rules.ToString();
            this.ComponentIcon = Resources.certmgr_445;
            this.ComponentImage = Resources.ApproveForm;
            this.ComponentName = "Code Rule";
            this.Description = "Evaluate object using c# code snippet";

            this._Instruction = new CodeRuleInstruction(this.ComponentID, Guid.Empty, string.Empty,
                "Misc", string.Empty, typeof(string).FullName,
                string.Empty, new RuleDecision(), string.Empty);
            this._Output = new CodeRuleOutput(this.ComponentID, Guid.Empty, string.Empty,
                typeof(string).FullName, string.Empty, false);
        }
        #endregion

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "RuleName", this.RuleName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CategoryName", this.CategoryName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "AssemblyName", this.InputAssemblyName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "InputTypeName", this.InputTypeFullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ContinueIfTrue", this.Decision.ContinueIfTrue.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ContinueIfFalse", this.Decision.ContinueIfFalse.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Code", Base64Util.EncodeAsBase64(this.Code, false));
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SerializedInputObject", this.SerializedInputObject);

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                CodeRuleComponent component = base.Instantiate(xNode) as CodeRuleComponent;

                component.RuleName = XmlDataUtil.GetAttributeValue(xNode, "RuleName", "");
                component.CategoryName = XmlDataUtil.GetAttributeValue(xNode, "CategoryName", "Misc");
                component.InputAssemblyName = XmlDataUtil.GetAttributeValue(xNode, "AssemblyName", "");
                component.InputTypeFullName = XmlDataUtil.GetAttributeValue(xNode, "InputTypeName", typeof(string).FullName);
                RuleDecision decision = new RuleDecision();
                decision.ContinueIfTrue = bool.Parse(XmlDataUtil.GetAttributeValue(xNode, "ContinueIfTrue", false.ToString()));
                decision.ContinueIfFalse = bool.Parse(XmlDataUtil.GetAttributeValue(xNode, "ContinueIfFalse", false.ToString()));
                component.Decision = decision;
                component.Code = Base64Util.DecodeAsString(XmlDataUtil.GetAttributeValue(xNode, "Code", ""), false);
                component.SerializedInputObject = XmlDataUtil.GetAttributeValue(xNode, "SerializedInputObject", "");

                return component;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }
    }
}
